data structures dp math *3200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#define N 200005

#define MOD 1000000007

#define pb push_back

int T,n,s,ans,a[N],ord[N],ps[N],vl[N],w1[N],w2[N],w[N];

int add(int x,int y) {x+=y;return x<MOD?x:x-MOD;}

bool cmp(int x,int y) {return a[x]==a[y]?x>y:a[x]<a[y];}

void clear() {s=0;for(int i=1;i<=n;++i) vl[i]=0;}

void upd(int x,int w) {s=add(s,w);for(;x<=n;x+=x&-x) vl[x]=add(vl[x],w);}

int qSm(int x) {int res=0;for(;x;x-=x&-x) res=add(res,vl[x]);return res;}

void slv()

{

	scanf("%d",&n);ans=0;for(int i=1;i<=n;++i) ord[i]=i,scanf("%d",&a[i]);

	sort(ord+1,ord+n+1,cmp);for(int i=1;i<=n;++i) a[ord[i]]=i;

	clear();for(int i=1;i<=n;++i) w1[i]=qSm(a[i])+1,upd(a[i],w1[i]); 

	clear();for(int i=n;i;--i) w2[i]=add(s,MOD-qSm(a[i]))+1,upd(a[i],w2[i]);

	for(int i=1,t=n;i<=n;++i) {while(t && a[t]<i) --t;ps[i]=t;}clear();

	for(int i=n;i;--i) if(ps[a[i]]<=i) w[i]=1;else

		w[i]=add(qSm(a[ps[a[i]]]),MOD-qSm(a[i]-1))+1,upd(a[i],w[i]);

	for(int i=1;i<=n;++i) ans=add(ans,1ll*w1[i]*add(w2[i],MOD-w[i])%MOD);

	printf("%d\n",ans);

}

int main() {scanf("%d",&T);while(T--) slv();return 0;}


Comments

Submit
0 Comments
More Questions

961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers